package com.group.itms.routes.provider;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.group.itms.routes.data.Advertisement;
import com.group.itms.routes.data.Topic;
import com.group.itms.routes.json.JSONParser;
import com.group.itms.routes.json.ServiceInfo;
import com.group.itms.routes.stub.BObjectOptionStub;
import com.group.itms.routes.stub.BObjectStub;
import com.group.itms.routes.stub.BObjectTopicStub;
import com.group.itms.routes.stub.ObjectGpsStub;
import com.group.itms.routes.stub.ObjectMapStub;
import com.group.itms.routes.stub.OptionStub;

public abstract class DataProvider {

	public static DataProvider getInstance() {
		return new DataProviderImpl(JSONParser.getInstance(), ServiceInfo.getInstance());
	}

	public abstract String lastModified();
	
	public abstract List<BObjectStub> getObjects() throws ProviderException;

	public abstract List<Topic> getTopics() throws ProviderException;

	public abstract List<OptionStub> getOptions() throws ProviderException;

	public abstract List<BObjectOptionStub> getObjectOptions() throws ProviderException;

	public abstract List<BObjectTopicStub> getObjectTopics() throws ProviderException;

	public abstract List<ObjectGpsStub> getGpsObjects() throws ProviderException;
	
	public abstract List<ObjectMapStub> getObjectMaps() throws ProviderException;
	
	public abstract List<Advertisement> getAdvertisements() throws ProviderException;
	
	public abstract void markCompleted();
	
	public abstract void setIgnoreErrors(boolean ignoreErrors);
	
	public abstract int getErrors();
	
	static class MockDataProvider extends DataProvider {

		private int nObjects;
		private int nTopics;
		private int nOptions;

		public MockDataProvider(int nObjs, int nTpcs, int nOpts) {
			nObjects = nObjs;
			nTopics = nTpcs;
			nOptions = nOpts;
		}

		@Override
		public String lastModified() {
			return "";
		}
		
		@Override
		public List<BObjectStub> getObjects() {
			List<BObjectStub> objects = new ArrayList<BObjectStub>(nObjects);
			for (int i = 0; i < nObjects; i++) {
				BObjectStub obj = new BObjectStub();
				obj.setActive(true);
				obj.setId(i + 1);
				obj.setMainFile("");
				obj.setMainPhoto("");
				obj.setObjInd("A");
				obj.setParentId(0);
				double r = Math.random();
				if (r >= 0 && r < 0.25) {
					obj.setName("Task" + (i + 1));
					obj.setText("Task");
					obj.setTypeId(4);
				}
				if (r >= 0.25 && r < 0.5) {
					obj.setName("Gldanis tba" + (i + 1));
					obj.setText("Gldanis tba");
					obj.setTypeId(1);
				}
				if (r >= 0.5 && r < 0.75 ) {
					obj.setName("Route " + (i + 1));
					obj.setText("Route");
					obj.setTypeId(2);
				}
				if (r >= 0.75 && r < 1) {
					obj.setName("Inf Point " + (i + 1));
					obj.setText("Inf Point");
					obj.setTypeId(3);
				}
				objects.add(obj);
			}
			return objects;
		}

		@Override
		public List<Topic> getTopics() {
			List<Topic> topics = new ArrayList<Topic>(nTopics);
			for (int i = 0; i < nTopics; i++) {
				Topic topic = new Topic();
				topic.setActive(true);
				topic.setId(i + 1);
				topic.setInsertTime(new Date());
				topic.setName("Topic" + (i + 1));
				topics.add(topic);
			}
			return topics;
		}

		@Override
		public List<OptionStub> getOptions() {
			List<OptionStub> options = new ArrayList<OptionStub>();
			for (int i = 0; i < nOptions; i++) {
				OptionStub opt = new OptionStub();
				opt.setId(i + 1);
				opt.setActive(true);
				opt.setInputType(1);
				opt.setName("Jora");
				opt.setObjectTypeId((int) (Math.random() * 5));
				opt.setSort(i + 1);
				options.add(opt);
			}
			return options;
		}

		@Override
		public List<BObjectOptionStub> getObjectOptions() {
			List<BObjectOptionStub> objectOptions = new ArrayList<BObjectOptionStub>();
			Random r = new Random();
			for (int i = 0; i < 5; i++) {
				BObjectOptionStub oo = new BObjectOptionStub();
				oo.setId(i + 1);
				oo.setObjectId(r.nextInt(nObjects) + 1);
				oo.setOptionId(r.nextInt(nOptions) + 1);
				oo.setText("Jorikela");
				objectOptions.add(oo);
			}
			return objectOptions;
		}

		@Override
		public List<BObjectTopicStub> getObjectTopics() {
			List<BObjectTopicStub> objectTopics = new ArrayList<BObjectTopicStub>();
			Random r = new Random();
			for (int i = 0; i < 6; i++) {
				BObjectTopicStub ot = new BObjectTopicStub();
				ot.setId(i + 1);
				ot.setObjectId(r.nextInt(nObjects) + 1);
				ot.setTopicId(r.nextInt(nTopics) + 1);
				objectTopics.add(ot);
			}
			return objectTopics;
		}

		@Override
		public List<ObjectGpsStub> getGpsObjects() throws ProviderException {
			List<ObjectGpsStub> objectGpses = new ArrayList<ObjectGpsStub>();
			return objectGpses;
		}

		@Override
		public void markCompleted() {
			
		}

		@Override
		public List<ObjectMapStub> getObjectMaps() throws ProviderException {
			return new ArrayList<ObjectMapStub>();
		}

		@Override
		public List<Advertisement> getAdvertisements() throws ProviderException {
			return new ArrayList<Advertisement>();
		}
		
		@Override
		public void setIgnoreErrors(boolean ignoreErrors) {
			
		}

		@Override
		public int getErrors() {
			return 0;
		}

	}

}
